using System;
using System.Collections.Generic;
using System.Text;

using System.Management.Automation;

namespace Microsoft.PowerShell.CmdletManagement
{
    [Cmdlet(VerbsCommon.Remove, "PsSpecProject", SupportsShouldProcess = true, DefaultParameterSetName="ById")]
    public class RemovePsSpecProjectCommand : PsSpecCommandBase
    {
        // ById Parameter Set
        [Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = "ById")]
        public int Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
                idSpecified = true;
            }
        }
        private int _id = -1;
        private bool idSpecified = false;

        // ByName Parameter Set
        [Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true, ParameterSetName = "ByName")]
        [ValidateLength(1, 50)]
        public String Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        private string _name = null;

        [Parameter]
        public SwitchParameter Force
        {
            get
            {
                return _force;
            }
            set
            {
                _force = value;
            }
        }
        private SwitchParameter _force;

        protected override void ProcessRecord()
        {
            // Remove the project if specified by name
            if (! idSpecified)
            {
                foreach (PsSpecProject project in GetProjects(Name))
                {
                    RemoveProject(project.Id);
                }
            }
            else
            {
                RemoveProject(Id);
            }
        }

        private IEnumerable<PsSpecProject> GetProjects(string name)
        {
            // Check that they've specified a valid project name
            if (!PsSpecCmdletUtils.IsValidProjectName(name))
            {
                WriteError(new ErrorRecord(
                    new Exception(resources.InvalidProjectName),
                    "INVALID_PROJECT_NAME",
                    ErrorCategory.InvalidArgument, Name));
                yield break;
            }

            GetPsSpecProjectCommand command = new GetPsSpecProjectCommand();
            command.Name = name;
            command.ServiceUri = ServiceUri;

            System.Collections.ArrayList results = new System.Collections.ArrayList();
            foreach (Object result in command.Invoke())
            {
                results.Add(result);
            }

            // Check that the project exists
            if (results.Count == 0)
            {
                string errorMessage = String.Format(resources.ProjectNotFound, name);

                WriteError(new ErrorRecord(
                    new Exception(errorMessage),
                    "PROJECT_NOT_FOUND",
                    ErrorCategory.ObjectNotFound, name));
                yield break;
            }

            foreach (Object result in results)
            {
                yield return (PsSpecProject) result;
            }
        }


        private void RemoveProject(int id)
        {
            // Try to process the removal
            if (ShouldProcess(
                String.Format("{0}", ServiceUri),
                String.Format(resources.RemovePsSpecProjectShouldProcess, id)))
            {
                if (_force || ShouldContinue(
                    String.Format(resources.ReallyWantToRemoveProjectText, id),
                    resources.ReallyWantToRemoveProjectTitle))
                {
                    try
                    {
                        Service.RemoveProject(id);
                    }
                    catch (System.Web.Services.Protocols.SoapException e)
                    {
                        WriteError(new ErrorRecord(e, "SERVICE_ERROR", ErrorCategory.InvalidOperation, Name));
                    }
                }
            }
        }

        private bool VerifyProjectExists(string Name)
        {
            GetPsSpecProjectCommand projectCommand = new GetPsSpecProjectCommand();
            projectCommand.Name = Name;
            projectCommand.ServiceUri = ServiceUri;

            bool projectFound = projectCommand.Invoke().GetEnumerator().MoveNext();
            return projectFound;
        }
    }
}
